package com.ruoyi.controller.admin.customer;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.admin.AdminCustomerDB;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.admin.AdminCustomerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/admin/customer")
public class AdminCustomerController extends BaseController {
    @Resource
    private AdminCustomerService adminCustomerService;

    /**
     * 查询后台-客户列表
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdminCustomerDB adminCustomer) {
//        startPage();
//        adminCustomer.setTenantId(getTenantId());
        return adminCustomerService.selectAdminCustomerList(adminCustomer);
    }

    /**
     * 导出后台-客户列表
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:export')")
    @Log(title = "后台-客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdminCustomerDB adminCustomer) {
        List<AdminCustomerDB> list = (List<AdminCustomerDB>) adminCustomerService.selectAdminCustomerList(adminCustomer).getRows();
        ExcelUtil<AdminCustomerDB> util = new ExcelUtil<AdminCustomerDB>(AdminCustomerDB.class);
        util.exportExcel(response, list, "后台-客户数据");
    }

    /**
     * 获取后台-客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(adminCustomerService.selectAdminCustomerById(id));
    }

    /**
     * 新增后台-客户
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:add')")
    @Log(title = "后台-客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdminCustomerDB adminCustomer) {
        return toAjax(adminCustomerService.insertAdminCustomer(adminCustomer));
    }

    /**
     * 修改后台-客户
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:edit')")
    @Log(title = "后台-客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdminCustomerDB adminCustomer) {
        return toAjax(adminCustomerService.updateAdminCustomer(adminCustomer));
    }

    /**
     * 删除后台-客户
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:remove')")
    @Log(title = "后台-客户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(adminCustomerService.deleteAdminCustomerByIds(ids));
    }

    /**
     * 升级客户
     */
    @PreAuthorize("@ss.hasPermi('customer:customer:edit')")
    @Log(title = "后台-客户", businessType = BusinessType.UPDATE)
    @GetMapping("handleLevel")
//    public AjaxResult handleLevel(@RequestParam("id") Long id, @RequestParam("intentionAddressNo") String intentionAddressNo, @RequestParam("amount") BigDecimal amount, @RequestParam("term") Integer term) {
//        return toAjax(adminCustomerService.handleLevel(id, intentionAddressNo, amount, term));
//    }
    public AjaxResult handleLevel(@RequestParam("id") Long id)
    {
        return toAjax(adminCustomerService.handleLevel(id));
    }
}
